//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_vehicleMobileRespawn.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);
private ["_veh","_var","_set","_type","_pos","_dir","_respawn","_respawn_time","_respawn_on_place","_display_name","_ammo_loaded"];
_veh  = _this select 0;
_var  = _this select 1;
_side  = _this select 2;
_set = ("BTC_mobile_" + _side);
_veh setVariable [_set,_var,true];
_display_name = localize _var;
if (_display_name == "") then { _display_name = _var };
_veh setVariable ["BTC_mobile_Name_" + _side, _display_name,true];
_type = typeOf _veh;
_pos  = getPos _veh;
_dir  = getDir _veh;
_respawn = _veh getVariable ["BTC_RESPAWN",(BTC_mobile_respawn == 1)];
_respawn_time = _veh getVariable ["BTC_RESPAWN_TIME",BTC_mobile_respawn_time];
_respawn_on_place = _veh getVariable ["BTC_RESPAWN_ON_PLACE",(BTC_mobile_respawn_on_place == 1)];
_ammo_loaded = _veh getVariable ["BTC_AMMO_LOADED",(_veh isKindOf "LandVehicle" || _veh isKindOf "Helicopter_Base_F")];
_veh setVariable ["BTC_AMMO_LOADED",_ammo_loaded,true];
if (!_respawn) then {
	TRACE_1("No Respawn",_veh);
} else {
	waitUntil {sleep 1.348;
		_ammo_loaded = _veh getVariable ["BTC_AMMO_LOADED",true];
		!Alive _veh
	};
	if (_respawn_on_place) then {
		_pos  = getPos _veh;
		_dir  = getDir _veh;
	};
	_veh setVariable [_set,0,true];
	sleep _respawn_time;
	deleteVehicle _veh;
	_veh = createVehicle [_type, (_pos findEmptyPosition [0,50,_type]),[],0,"NONE"];
	_veh setDir _dir;
	_veh setVelocity [0, 0, -1];
	_veh setVariable [_set,_var,true];
	_veh setVariable ["BTC_RESPAWN_ON_PLACE",_respawn_on_place,true];
	_veh setVariable ["BTC_AMMO_LOADED",_ammo_loaded,true];
	[_veh,_var,_side] spawn FUNC(vehicleMobileRespawn);
	if (BTC_mobile_teleport == 1) then {
		["BTCR_pveh_vec",[100,_side,_veh,_var]] call FUNC(callEvent);
	};
	if (BTC_mobile_heal == 1) then {
		["BTCR_pveh_vec",[101,_side,_veh,_var]] call FUNC(callEvent);
	};
	if (BTC_mobile_ammo == 1) then {
		["BTCR_pveh_vec",[102,_side,_veh,_var]] call FUNC(callEvent);
	};
	TRACE_1("End",_veh);
};